package com.eric.server.packet;

import com.eric.server.logic.packet.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public enum PacketType {

	/* 请求--链接心跳包 */
	HeartBeat(1000, HeartBeatPacket.class),
	/* 推送--新用户注册  */
	UserRegister(2000, UserRegisterPacket.class),
	/* 推送--新用户注册  */
	UserOut(2001, UserOutPacket.class),
    /* 请求--群聊  */
	ChatToGroup(3000, ChatToGroupPacket.class),



	NoticeClient(5000, NoticePacket.class),
	;

	private int type;
	private Class<? extends Packet> packetClass;
	private static Map<Integer,Class<? extends Packet>> PACKET_CLASS_MAP = new HashMap<Integer,Class<? extends Packet>>();

	public static void initPackets() {
		Set<Integer> typeSet = new HashSet<Integer>();
		Set<Class<?>> packets = new HashSet<Class<?>>();
		for(PacketType p:PacketType.values()){
			int type = p.getType();
			if(typeSet.contains(type)){
				throw new IllegalStateException("packet type 协议类型重复"+type);
			}
			Class<?> packet = p.getPacketClass();
			if (packets.contains(packet)) {
				throw new IllegalStateException("packet定义重复"+p);
			}
			PACKET_CLASS_MAP.put(type,p.getPacketClass());
			typeSet.add(type);
			packets.add(packet);
		}
	}

	PacketType(int type,Class<? extends Packet> packetClass){
		this.setType(type);
		this.packetClass = packetClass;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Class<? extends Packet> getPacketClass() {
		return packetClass;
	}

	public void setPacketClass(Class<? extends Packet> packetClass) {
		this.packetClass = packetClass;
	}


	public static Class<? extends Packet> getPacketClassBy(int packetType){
		return PACKET_CLASS_MAP.get(packetType);
	}

	public static void main(String[] args) {
		for(PacketType p:PacketType.values()){
			System.err.println(p.getPacketClass().getSimpleName());
		}
	}

}